//
//  SweepViewController.m
//  MLF
//
//  Created by lanou on 15/9/12.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "SweepViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface SweepViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (strong, nonatomic) AVCaptureSession *session;

@property (strong, nonatomic) NSTimer *timer;

@property (strong, nonatomic) UIImageView *lineImage;

@property (strong, nonatomic) UIImageView * image;

@end

@implementation SweepViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor cyanColor];


//     初始化
    
    //获取摄像设备
    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //创建输入流
    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    //创建输出流
    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
    //设置代理 在主线程里刷新
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    //初始化链接对象
    self.session = [[AVCaptureSession alloc]init];
    //高质量采集率
    [self.session setSessionPreset:AVCaptureSessionPresetHigh];
    
    [self.session addInput:input];
    [self.session addOutput:output];
    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)
    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
    
    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
    layer.frame=self.view.layer.bounds;
    [self.view.layer insertSublayer:layer atIndex:0];

    //开始捕获
    [self.session startRunning];

    
    // 扫描的感应框位置

//    UIView *downView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 4 * 3, self.view.frame.size.width, self.view.frame.size.height / 4)];
//    [self.view addSubview:downView];
//    downView.backgroundColor = [UIColor blackColor];
//

    self.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick.jpg"]];
    self.image.frame = CGRectMake(20, self.view.frame.size.height / 2 - 140, 280, 280);
    [self.view addSubview:self.image];

    self.lineImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line.jpg"]];
    self.lineImage.frame = CGRectMake(0, 0, self.image.frame.size.width, 10);
    [self.image addSubview:self.lineImage];

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autolineRemove) object:nil];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:8.1 target:self selector:@selector(autolineRemove) userInfo:nil repeats:YES];


    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 280, 40)];
    label.text = @"请将扫描的二维码至于下面的框内\n谢谢！";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.lineBreakMode = 0;
    label.numberOfLines = 2;
    //    label.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label];

    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height - 100, 80, 80);
    [cancleBtn setImage:[UIImage imageNamed:@"sweep-back"] forState:UIControlStateNormal];
    cancleBtn.layer.cornerRadius = 10;
    //    [cancleBtn setTitle:@"Cancle" forState:UIControlStateNormal];
    //    cancleBtn.backgroundColor = [UIColor redColor];
    [cancleBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancleBtn];


}


- (void)back
{

    [self.timer invalidate];
    [self dismissViewControllerAnimated:NO completion:nil];

}


- (void)autolineRemove
{
    [UIView animateWithDuration:4.0 animations:^{
        self.lineImage.center = CGPointMake(self.image.frame.size.width / 2, self.image.frame.size.height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:4.0 animations:^{
            self.lineImage.center = CGPointMake(self.image.frame.size.width / 2, 0);
        }];
    }];

}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects.count>0) {
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
        //输出扫描字符串
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:metadataObject.stringValue]];
    }
}





@end
